package com.topisv.tms.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.topisv.tms.entity.QueryParamHolder;
import com.topisv.tms.entity.ShopSource;
import com.topisv.tms.repository.ShopSourceDao;
import com.topisv.tms.service.base.BaseService;

//Spring Bean的标识.
@Component
// 默认将类中的所有public函数纳入事务管理.
@Transactional(readOnly = true)
public class ShopSourceService extends BaseService{

	@Autowired
	private ShopSourceDao shopSourceDao;
	
	
	public ShopSource findByYhdStoreId(String yhdStoreId){
		return shopSourceDao.findByYhdStoreId(yhdStoreId);
	}
	
	public List<ShopSource> findByCustomerCodeAndOrdenoSourceCode(String customerCode,String ordenoSourceCode){
		return shopSourceDao.findByCustomerCodeAndOrdenoSourceCode(customerCode, ordenoSourceCode);
	}
	
	public List<ShopSource> findByOrdenoSourceCode(String ordenoSourceCode){
		return shopSourceDao.findByOrdenoSourceCode(ordenoSourceCode);
	}
	
	public ShopSource findByShopCode(String shopCode){
		return shopSourceDao.findByShopCode(shopCode);
	}
	
	public ShopSource findByHtShopId(String shopID){
		return shopSourceDao.findByHtShopId(shopID);
	}
	
	public ShopSource findShopName(String shopNam){
		return shopSourceDao.findByShopName(shopNam);
	}

	public ShopSource findByJdKeyAndJdSecret(String jdKey, String jdSecret) {
		return shopSourceDao.findByJdKeyAndJdSecret(jdKey, jdSecret);
	}

	public List<ShopSource> findByCustomerCode(String customerCode) {
		return shopSourceDao.findByCustomerCode(customerCode);
	}
	
	public List<ShopSource> findByGuaranteeStatus() {
		return shopSourceDao.findByGuaranteeStatus();
	}
	public List<ShopSource> findByGuaranteeStatus(String customerCode) {
		return shopSourceDao.findByGuaranteeStatusAndCustomerCode(customerCode);
	}
	@Transactional(readOnly = false)
	public void delete(Long id) throws Exception {
		try {
			ShopSource obj = shopSourceDao.findOne(id);
			obj.setStatus("delete");
			shopSourceDao.save(obj);
		} catch (Exception e) {
			throw e;
		}
	}

	public ShopSource findOne(Long id) throws Exception {
		try {
			
			ShopSource shopSource= shopSourceDao.findOne(id);
			if(shopSource!=null){
				String expressCodes=shopSource.getExpressCodes();
				if(expressCodes!=null){
					List<String> lists=new ArrayList<String>();
					String[] codes=expressCodes.split(",");
					for(String code:codes){
						lists.add(code);
					}
					shopSource.setListExpressCodes(lists);
				}
				
			}
			
			return shopSource;
		} catch (Exception e) {
			throw e;
		}
	}

	@Transactional(readOnly = false)
	public ShopSource save(ShopSource arg0) throws Exception {
		try {
			return shopSourceDao.save(arg0);
		} catch (Exception e) {
			throw e;
		}
	}

	@Transactional(readOnly = false)
	public List<ShopSource> save(List<ShopSource> arg0) throws Exception {
		try {
			return (List<ShopSource>) shopSourceDao.save(arg0);
		} catch (Exception e) {
			throw e;
		}
	}

	public Page<ShopSource> pageShopSourcePage(QueryParamHolder paramHolder) throws Exception {
		return findByJson(paramHolder, shopSourceDao, ShopSource.class);
	}

	public ShopSource findByTopisvNick(String topisvNick) {
		return shopSourceDao.findByTopisvNick(topisvNick);
	}

	public ShopSource findByTopisvCode(String topisvCode) {
		return shopSourceDao.findByTopisvCode(topisvCode);
	}
}
